<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户注册</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/layui@2.7.4/dist/css/layui.css">
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            background-color: #f0f8ff; /* 背景颜色 */
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
        }
        .register-container {
            width: 400px; /* 调整宽度 */
            padding: 30px; /* 增加内边距 */
            background-color: #fff;
            border-radius: 10px; /* 圆角边框 */
            box-shadow: 0 0 15px rgba(0, 0, 0, 0.1); /* 增加阴影效果 */
            text-align: center; /* 文本居中 */
            transition: all 0.3s ease;
        }
        .register-container:hover {
            box-shadow: 0 0 20px rgba(0, 0, 0, 0.2);
        }
        .register-container h2 {
            margin-bottom: 20px;
            color: #333;
            font-weight: 600;
        }
        .layui-form-item {
            margin-bottom: 20px;
            width: 100%;
            display: flex;
            flex-direction: column;
            align-items: center;
        }
        .layui-form-label {
            margin-bottom: 5px;
            width: 100%;
            text-align: center;
        }
        .layui-input-block {
            width: 100%;
            display: flex;
            justify-content: center;
        }
        .layui-input {
            width: 60%; /* 输入框宽度 */
            margin-left: -106px;
            padding: 12px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 16px;
            transition: border-color 0.3s ease;
            text-align: center; /* 输入框文字居中 */
        }
        .layui-input:focus {
            border-color: #5FB878; /* 输入框聚焦时的边框颜色 */
        }
        .layui-btn {
            width: 100%; /* 按钮宽度 */
            padding: 14px;
            font-size: 18px;
            background-color: #5FB878; /* 绿色按钮 */
            border: none;
            border-radius: 5px;
            color: #fff;
            cursor: pointer;
            transition: background-color 0.3s ease;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        .layui-btn:hover {
            background-color: #51a351;
        }
        .login-link {
            display: block;
            margin-top: 10px;
            font-size: 14px;
            color: #5FB878;
            text-decoration: none;
            transition: color 0.3s ease;
        }
        .login-link:hover {
            text-decoration: underline;
            color: #4cae4c;
        }
        .error-message {
            color: red;
            font-size: 14px;
            margin-top: 5px;
        }
    </style>
</head>
<body>
<%
    // 从请求参数中获取注册结果信息
    String message = request.getParameter("message");
%>
<div class="register-container">
    <h2>用户注册</h2>
    <% if ("success".equals(message)) { %>
    <p style="color: green; font-size: 18px;">您已成功注册！5秒后为您跳转到登录窗口。</p>
    <script>
        // 注册成功后，5秒后自动跳转到登录页面
        setTimeout(function() {
            window.location.href = "login.jsp";
        }, 5000); // 5000毫秒 = 5秒
    </script>
    <% } %>
    <form class="layui-form" action="UserRegisterServlet" method="post" onsubmit="return validateForm()">
        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-block">
                <input type="text" name="username" required lay-verify="required" placeholder="请输入用户名" autocomplete="off" class="layui-input">
            </div>
            <% if ("usernameExists".equals(message)) { %>
            <div class="error-message">用户名已存在，请修改</div>
            <% } %>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-block">
                <input type="password" name="password" required lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">确认密码</label>
            <div class="layui-input-block">
                <input type="password" name="confirmPassword" required lay-verify="required" placeholder="请确认密码" autocomplete="off" class="layui-input">
            </div>
            <div id="passwordMismatchError" class="error-message" style="display: none;">两次密码输入不一致</div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">邮箱</label>
            <div class="layui-input-block">
                <input type="email" name="email" required lay-verify="required" placeholder="请输入邮箱" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <button class="layui-btn" lay-submit lay-filter="register">注册</button>
        </div>
        <a href="login.jsp" class="login-link">已有账号？立即登录</a>
    </form>
</div>

<script src="https://cdn.jsdelivr.net/npm/layui@2.7.4/dist/layui.js"></script>
<script>
    layui.use(['form'], function(){
        var form = layui.form;

        // 表单验证
        form.verify({
            username: function(value, item){ // value：表单的值、item：表单的DOM对象
                if(!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)){
                    return '用户名不能有特殊字符';
                }
                if(/(^\_)|(\__)|(\_+$)/.test(value)){
                    return '用户名首尾不能出现下划线“_”';
                }
                if(/^\d+\d+\d$/.test(value)){
                    return '用户名不能全为数字';
                }
            }
            ,password: [
                /^[\S]{6,12}$/
                ,'密码必须6到12位，且不能出现空格'
            ]
        });
    });

    // 验证两次密码是否一致
    function validateForm() {
        var password = document.querySelector('input[name="password"]').value;
        var confirmPassword = document.querySelector('input[name="confirmPassword"]').value;
        var errorMessage = document.getElementById('passwordMismatchError');

        if (password !== confirmPassword) {
            errorMessage.style.display = 'block'; // 显示错误信息
            return false; // 阻止表单提交
        } else {
            errorMessage.style.display = 'none'; // 隐藏错误信息
            return true; // 允许表单提交
        }
    }
</script>
</body>
</html>